package com.itheima.treemap;

import com.itheima.domain.Student;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/*
TreeMap 的使用
自定义比较器 年龄降序, 姓名的降序
 */
public class Demo02 {

    public static void main(String[] args) {
     /*   TreeMap<Student, String> map = new TreeMap<>(
                new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.getAge()-o1.getAge()==0?o2.getName().compareTo(o1.getName()):o2.getAge()-o1.getAge();
                    }
                }
        );*/
        TreeMap<Student, String> map = new TreeMap<>(
                (o1, o2) -> o2.getAge()-o1.getAge()==0?o2.getName().compareTo(o1.getName()):o2.getAge()-o1.getAge()
        );
        Student wu = new Student("wuyifan", 31);
        Student huang = new Student("huangzitao", 31);
        Student lu = new Student("luhan", 30);
        Student zhang = new Student("zhangyixing", 31);

        map.put(wu,"海淀监狱");
        map.put(huang,"泰然居");
        map.put(lu,"老军营");
        map.put(zhang,"汇大国际");

        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {

            System.out.println(entry.getKey().getName()+"住在"+entry.getValue());
        }
    }

}
